<?php

namespace App\Model;

use App\Observers\CommentObserver;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;

class Comment extends Model
{
    use LogsActivity;
    //设置记录的动态属性
    protected static $logFillable=true;
    //记录事件
    protected static $recordEvents=['created','updated'];
    //自定义日志名称
    protected static $logName='comment';

    protected $fillable=['content','user_id'];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function zan(){
        return $this->morphMany(Zan::class,'zan');
    }

    public function isZan(User $user){
        return $this->zan()->where('user_id',$user['id'])->first();
    }

    public static function boot()
    {
        self::observe(CommentObserver::class);
        parent::boot(); // TODO: Change the autogenerated stub
    }

    public function belongsModel(){
        return $this->morphTo('comment');
    }

    public function getMarkdownAttribute (  )
    {
        $Parsedown = new \Parsedown();

        return $Parsedown->text($this->content);
    }
}
